import { notification } from 'antd';
import * as structureServices from '../../services/design/structureServices';
export default {
    namespace: 'structure',
    state: {
        structures: [],
        refStructures: [],
        configingStructure: null,
        buildingStructures: {
            range: [1, 100],
            list: []
        },
        designedStructures: []
    },
    reducers: {
        setConfigStructure(state, { payload: structure }) {
            return { ...state, configingStructure: structure };
        },
        addStructure(state, { payload: structures }) {
            return { ...state, structures: [...(state.structures ? state.structures : []), ...structures] };
        },
        addrefStructure(state, { payload: structures }) {
            return { ...state, refStructures: [...(state.refStructures ? state.refStructures : []), ...structures] };
        },
        buildingStructure(state, { payload: { structure, range } }) {
            return { ...state, buildingStructures: { list: structure, range } };
        },
        setStructures(state, { payload: structures }) {
            return { ...state, structures: structures };
        },
        setRefStructures(state, { payload: structures }) {
            return { ...state, refStructures: structures };
        },
        setDesignedStructures(state, { payload: { values, onSuccess, onError } }) {
            if (onSuccess) {
                onSuccess("success")
            }

            return { ...state, designedStructures: values };
        },
    },
    effects: {
        *add({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(structureServices.addStructure, { ...values });
            if (res && res.data && res.data.code == "200") {
                onSuccess(res.data.data);
                yield put({
                    type: 'addStructure',
                    payload: res.data.data
                });
                notification.info({ message: "添加构件成功" });
            } else {
                onError(res && res.data ? res.data.message : '服务器未连接');
            }

        },
        *uploadAndAnalyzing({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(structureServices.uploadAndAnalyzing, values);
            if (res && res.data && res.data.code == "200") {
                onSuccess(res.data.data);
                notification.info({ message: "上传文件成功" });
            } else {
                onError(res && res.data ? res.data.message : '服务器未连接');
            }

        },
        *deleteDefinition({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(structureServices.deleteDefinition, values);
            if (res && res.data && res.data.code == "200") {
                onSuccess(res.data.data);
                notification.info({ message: "删除构件成功" });
            } else {
                onError(res && res.data ? res.data.message : '服务器未连接');
            }

        },
        *addDefinition({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(structureServices.addDefinition, values);
            if (res && res.data && res.data.code == "200") {
                onSuccess(res.data.data);
                notification.info({ message: "添加构件属性成功" });
            } else {
                onError(res && res.data ? res.data.message : '服务器未连接');
            }

        },
        *updateDefinition({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(structureServices.updateDefinition, values);
            if (res && res.data && res.data.code == "200") {
                onSuccess(res.data.data);
                notification.info({ message: "更新构件属性成功" });
            } else {
                onError(res && res.data ? res.data.message : '服务器未连接');
            }

        },
        *getDefinitionPropertyByDefinitionId({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getDefinitionPropertyByDefinitionId, values);
            if (res && res.data && res.data.code == "200") {

            } else {
            }
            return res&&res.data&&res.data.data?res.data.data:null
        },
        *addBottomProperty({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(structureServices.addBottomProperty, values);
            if (res && res.data && res.data.code == "200") {
                onSuccess(res.data.data);
                notification.info({ message: "设置构件属性成功" });
            } else {
                onError(res && res.data ? res.data.message : '服务器未连接');
            }

        },
        *queryStructures({ payload:values }, { call, put, select }) {
            const res = yield call(structureServices.queryStructures, values);
            if (res && res.data && res.data.code == "200") {
              
            } else {
                onError(res && res.data ? res.data.message : '服务器未连接');
            }
            return res&&res.data&&res.data.data?res.data.data:[]

        },
        *getCateTree({ payload:values }, { call, put, select }) {
            const res = yield call(structureServices.getCateTree, values);
            if (res && res.data && res.data.code == "200") {
              
            } else {
                onError(res && res.data ? res.data.message : '服务器未连接');
            }
            return res&&res.data&&res.data.data?res.data.data:[]

        },
        *getByBearingPlinthIds({ payload:values }, { call, put, select }) {
            const res = yield call(structureServices.getByBearingPlinthIds, values);
            if (res && res.data && res.data.code == "200") {
              
            } else {
                onError(res && res.data ? res.data.message : '服务器未连接');
            }
            return res&&res.data&&res.data.data?res.data.data:[]

        },
        *ref({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(structureServices.refStructure, values);
            if (res && res.data && res.data.code == "200") {
                onSuccess(res.data.data);
                yield put({
                    type: 'addrefStructure',
                    payload: res.data.data
                });
                notification.info({ message: "引用构件成功" });
            } else {
                onError(res && res.data ? res.data.message : '服务器未连接');
            }

        },
        *updateConfigStructure({ payload: structure }, { call, put, select }) {
            yield put({
                type: 'setConfigStructure',
                payload: structure
            });
        },
        *updateStructures({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(structureServices.queryStructures, { ...values });
            if (res && res.data && res.data.code == "200") {

                yield put({
                    type: 'setStructures',
                    payload: res.data.data || []
                });
            } else {
                yield put({
                    type: 'setStructures',
                    payload: []
                });
            }

        },
        *deleteDefinitionInstance({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(structureServices.deleteDefinitionInstance, { ...values });
            if (res && res.data && res.data.code == "200") {
                notification.info({ message: "删除构造物成功!" })
                if (onSuccess) {
                    onSuccess(res.data.data)
                }
            } else {
                if (onError) {
                    onError(res.data.data)
                }
            }

        },
        *deleteBatchBearingPlinthSub({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(structureServices.deleteBatchBearingPlinthSub, { ...values });
            if (res && res.data && res.data.code == "200") {
                notification.info({ message: "删除支座成功!" })
                if (onSuccess) {
                    onSuccess(res.data.data)
                }
            } else {
                if (onError) {
                    onError(res.data.data)
                }
            }

        },
        *updateBatchDefinitionInstanceProperties({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(structureServices.updateBatchDefinitionInstanceProperties, { ...values });
            if (res && res.data && res.data.code == "200") {
                notification.info({ message: "更新构造物属性成功!" })
                if (onSuccess) {
                    onSuccess(res.data.data)
                }
            } else {
                if (onError) {
                    onError(res.data.data)
                }
            }

        },
        *updateBatchTopDefinitionInstanceProperties({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(structureServices.updateBatchTopDefinitionInstanceProperties, { ...values });
            if (res && res.data && res.data.code == "200") {
                notification.info({ message: "更新构造物属性成功!" })
                if (onSuccess) {
                    onSuccess(res.data.data)
                }
            } else {
                if (onError) {
                    onError(res.data.data)
                }
            }

        },
        *updateDefinitionInstanceProperties({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(structureServices.updateDefinitionInstanceProperties, { ...values });
            if (res && res.data && res.data.code == "200") {
                notification.info({ message: "更新构造物属性成功!" })
                if (onSuccess) {
                    onSuccess(res.data.data)
                }
            } else {
                notification.info({ message:res.data.message})
                if (onError) {
                    onError(res.data.data)
                }
            }

        },

        *addDefinitionInstance({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(structureServices.addDefinitionInstance, { ...values });
            if (res && res.data && res.data.code == "200") {
                if (onSuccess) {
                    onSuccess(res.data.data)
                }
                notification.info({ message: "设置构造物成功!" })
            } else {
                if (onError) {
                    onError(res.data.data)
                }
            }
        },
        *replaceDefinitionInstance({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(structureServices.replaceDefinitionInstance, { ...values });

            if (res && res.data && res.data.code == "200") {

                if (onSuccess) {
                    onSuccess(res.data.data)
                }
                notification.info({ message: "更新构造物成功!" })
            } else {
                if (onError) {
                    onError(res.data.data)
                }
            }
        },
        *addBachBearingPlinth({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(structureServices.addBachBearingPlinth, { ...values });

            if (res && res.data && res.data.code == "200") {

                if (onSuccess) {
                    onSuccess(res.data.data)
                }
                notification.info({ message: "添加支座成功!" })
            } else {
                if (onError) {
                    onError(res.data.data)
                }
            }
        },
        *updateBearingPlinthSub({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(structureServices.updateBearingPlinthSub, { ...values });

            if (res && res.data && res.data.code == "200") {

                if (onSuccess) {
                    onSuccess(res.data.data)
                }
                notification.info({ message: "更新支座成功!" })
            } else {
                if (onError) {
                    onError(res.data.data)
                }
            }
        },
        *getBearingPlinthByBridgeId({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getBearingPlinthByBridgeId, { ...values });
            if (res && res.data && res.data.code == "200") {
            } else {

            }
            return res && res.data && res.data.data ? res.data.data : []
        },
        *getBottomDefinitionInstanceList({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getBottomDefinitionInstanceList, { ...values });
            if (res && res.data && res.data.code == "200") {
            } else {

            }
            return res && res.data && res.data.data ? res.data.data : []
        },
        *getDefinitionInstanceDetail({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getDefinitionInstanceDetail, { ...values });
            if (res && res.data && res.data.code == "200") {
            } else {

            }
            return res && res.data && res.data.data ? res.data.data : null
        },

        *getBotDefinitionInstanceListById({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getBotDefinitionInstanceListById, { ...values });
            if (res && res.data && res.data.code == "200") {
            } else {

            }
            return res && res.data && res.data.data ? res.data.data : []
        },
        *getBottomProperty({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getBottomProperty, { ...values });
            if (res && res.data && res.data.code == "200") {
            } else {

            }
            return res && res.data && res.data.data ? res.data.data : []
        },
        *getTopDefinitionInstanceListById({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getTopDefinitionInstanceListById, { ...values });
            if (res && res.data && res.data.code == "200") {
            } else {

            }
            return res && res.data && res.data.data ? res.data.data : []
        },
        *getTopDefinitionInstanceList({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getTopDefinitionInstanceList, { ...values });
            if (res && res.data && res.data.code == "200") {
            } else {

            }
            return res && res.data && res.data.data ? res.data.data : []
        },

        *getComponentBySpanId({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getComponentBySpanId, { ...values });
            if (res && res.data && res.data.code == "200") {
                // yield put({
                //     type: 'setStructures',
                //     payload: res.data.data||[]
                // });
            } else {
                // yield put({
                //     type: 'setStructures',
                //     payload:[]
                // });
            }
            return res && res.data && res.data.data ? res.data.data : []

        },
        *getExpansionJoint({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getExpansionJoint, { ...values });
            if (res && res.data && res.data.code == "200") {
                return res.data.data
            } else {
                return []
            }
        },
        *getBotStructuresByBridge({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getBotStructuresByBridge, { ...values });
            if (res && res.data && res.data.code == "200") {
                return res.data.data
            } else {
                return []
            }
        },
        *definitionTreeAttrList({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.definitionTreeAttrList, { ...values });
            if (res && res.data && res.data.code == "200") {
                return res.data.data
            } else {
                return []
            }
        },
        *getSectionComponents({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getSectionComponents, { ...values });
            if (res && res.data && res.data.code == "200") {
                return res.data.data
            } else {
                return []
            }
        },
        *updateTopComponentProps({ payload: {values,onSuccess, onError }}, { call, put, select }) {
            const res = yield call(structureServices.updateTopComponentProps, { ...values });
            if (res && res.data && res.data.code == "200") {
                if(onSuccess){
                    onSuccess(res.data.data)
                }
            } else {
                if(onError){
                    onError([])
                }
            }
        },
        *getBottomDefinitionTreeList({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getBottomDefinitionTreeList, { ...values });
            if (res && res.data && res.data.code == "200") {
                return res.data.data
            } else {
                return []
            }
        },
        *getTopDefinitionInstanceBySelectionList({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getTopDefinitionInstanceBySelectionList, { ...values });
            if (res && res.data && res.data.code == "200") {
                return res.data.data
            } else {
                return []
            }
        },
        *getTopDefinitionTreeList({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getTopDefinitionTreeList, { ...values });
            if (res && res.data && res.data.code == "200") {
                return res.data.data
            } else {
                return []
            }
        },
        *getTopStructureInstance({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getTopStructureInstance, { ...values });
            if (res && res.data && res.data.code == "200") {
                return res.data.data
            } else {
                return []
            }
        },
        *getDefinitionList({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getDefinitionList, { ...values });
            if (res && res.data && res.data.code == "200") {
                return res.data.data
            } else {
                return []
            }
        },
        *getDefinitionDetail({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getDefinitionDetail, { ...values });
            if (res && res.data && res.data.code == "200") {
                return res.data.data
            } else {
                return []
            }
        },
        *getTopStructuresByBridge({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getTopStructuresByBridge, { ...values });
            if (res && res.data && res.data.code == "200") {
                return res.data.data
            } else {
                return []
            }
        },
        *getStructureTree({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getStructureTree, { ...values });
            if (res && res.data && res.data.code == "200") {
                return res.data.data
            } else {
                return []
            }
        },
        *getBotStructures({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getBotStructures, { ...values });
            if (res && res.data && res.data.code == "200") {
                return res.data.data
            } else {
                return []
            }
        },
        *getTopStructures({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getTopStructures, { ...values });
            if (res && res.data && res.data.code == "200") {
                return res.data.data
            } else {
                return []
            }
        },
        *getTopComponent({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getTopComponent, { ...values });
            const offset = yield select(state => state.refRoads.offset)
            if (res && res.data && res.data.code == "200") {
                if (res.data.data && res.data.data.length) {
                    let data = res.data.data;
                    for (let i = 0; i < data.length; i++) {
                        let item = data[i];
                        if (item.spans && item.spans.length && offset) {
                            for (let j = 0; j < item.spans.length; j++) {
                                item.spans[j] = { ...item.spans[j], lines: [[item.spans[j].station - offset.PK, item.spans[j].elevation - offset.Z], [item.spans[j].station - offset.PK, item.spans[j].groundHeight - offset.Z]] }
                                item.spans[j].bridgeId = values.bridgeId
                            }
                        }
                    }
                }
            } else {

            }
            return res && res.data && res.data.data ? res.data.data : []

        },
        *getComponentDetail({ payload: values }, { call, put, select }) {
            const res = yield call(structureServices.getComponentDetail, { ...values });
            let data;
            if (res && res.data && res.data.code == '200') {
                data = res.data.data;
            }
            return data ? [data] : []
        },
        *updatebuildingStructures({ payload: { structures, range, onSuccess, onError } }, { call, put, select }) {
            yield put({
                type: 'buildingStructure',
                payload: { structures, range }
            });
            onSuccess('更新成功')
        },
        *queryStructureInfo({ payload: params }, { call, put, select }) {
            const res = yield call(structureServices.queryStructureInfo, params);
            if (res && res.data && res.data.code == "200") {

            }
        },

        *updateRefStructures({ payload: { params, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(structureServices.queryProjectStructures, params);
            if (res && res.data && res.data.code == "200") {
                if (onSuccess) {
                    onSuccess(res.data.data);
                }

                yield put({
                    type: 'setRefStructures',
                    payload: res.data.data || []
                });
            } else {
                yield put({
                    type: 'setRefStructures',
                    payload: []
                });
                if (onError) {
                    onError(res && res.data ? res.data.message : '服务器未连接');
                }

            }

        }
    }
}